package com.sohh.demo;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.stream.Stream;

/**
 * @description: reduce
 * @author: ruoan
 * @date: 2021/2/21 15:57
 */
public class Test10 {

    public static void main(String[] args) {

        //CompletableFuture<String> task1 = CompletableFuture.completedFuture("task1")
        //        .thenApply(s -> "task1的计算结果：s1 = " + s);
        //System.out.println(task1);
        //System.out.println(task1.getNow(null));


        //CompletableFuture<String> msg = CompletableFuture.completedFuture("message")
        //        .thenApplyAsync(s -> {
        //            try {
        //                Thread.sleep(800);
        //
        //            } catch (InterruptedException e) {
        //                e.printStackTrace();
        //            }
        //            return s + "result";
        //        });
        //final String now = msg.getNow(null);
        //System.out.println("now="+now);
        //
        //
        //final boolean cancel = msg.cancel(true);
        //System.out.println("cancel="+cancel);
        //String now1 = msg.getNow(null);
        //
        //try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); }
        //
        //
        CompletableFuture<String> task1 = CompletableFuture.completedFuture("task1")
                .thenApply(s -> "task1的计算结果：s1 = " + s);


        //同步有返回值
        //applyToEither的第二个参数接收的值是task1计算的返回值
        //final CompletableFuture<String> task2 = task1.applyToEither(CompletableFuture.completedFuture("task2")
        //        .thenApply(s -> "task2的计算结果：s2 = " + s), s -> s);
        //System.out.println("task2:" + task2.getNow(null));


        // 异步计算
        System.out.println("-------------------");
        //List<CompletableFuture<Void>> completableFutureList = new ArrayList<>();
        //final ExecutorService executorService = Executors.newFixedThreadPool(2);
        //for (int i = 0; i < 10; i++) {
        //    final CompletableFuture<Void> voidCompletableFuture = CompletableFuture.runAsync(() -> {
        //        System.out.println(Thread.currentThread().getName());
        //    },executorService);
        //    completableFutureList.add(voidCompletableFuture);
        //
        //}
        //completableFutureList.forEach(CompletableFuture::join);
        //System.out.println("hello world1!");







        



    }



}
